package com.kt.uokos.yimo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kt.uokos.yimo.dao.Product;
import com.kt.uokos.yimo.example.ProductExample;
import com.kt.uokos.yimo.mapper.ProductMapper;
import com.kt.uokos.yimo.service.ProductService;
import com.kt.uokos.yimo.util.RandomUtils;
import com.kt.uokos.yimo.util.ResponseUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

import static com.kt.uokos.yimo.util.AdminResponseCode.GOODS_NAME_EXIST;

/**
 * @Description TODO
 * @ClassName ProductServiceImpl
 * @Date 2021/12/16 下午3:34
 * @Author KangTao
 */
@Service
public class ProductServiceImpl  implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public List<Integer> queryCatId(Integer brandId, String keyword, Boolean isHot, Boolean isNew) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        ProductExample.Criteria criteria1 = example.createCriteria();
        if (!StringUtils.isEmpty(brandId)){
            criteria.andBrandIdEqualTo(brandId);
            criteria1.andBrandIdEqualTo(brandId);

        }
        if (!StringUtils.isEmpty(keyword)){
            criteria.andKeywordsLike("%"+keyword+"%");
            criteria1.andNameLike("%"+keyword+"%");
        }
        if (!StringUtils.isEmpty(isHot)){
            criteria.andIsHotEqualTo(isHot);
            criteria1.andIsHotEqualTo(isHot);

        }
        if (!StringUtils.isEmpty(isNew)){
            criteria.andIsNewEqualTo(isNew);
            criteria1.andIsNewEqualTo(isNew);

        }
        criteria.andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        criteria1.andIsOnSaleEqualTo(true).andDeletedEqualTo(false);

        List<Product> productList = productMapper.selectByExample(example);
        List<Integer> catIdList = new ArrayList<>();
        for (Product product : productList) {
            catIdList.add(product.getCategoryId());
        }
        return catIdList;
    }

    @Override
    public List<Product> querySelective(Integer categoryId, Integer brandId, String keyword, Boolean isNew, Boolean isHot, Integer page, Integer limit, String sort, String order) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.or();
        ProductExample.Criteria criteria2 = example.or();
        if (!StringUtils.isEmpty(categoryId) && categoryId!=0){
            criteria.andCategoryIdEqualTo(categoryId);
            criteria2.andCategoryIdEqualTo(categoryId);
        }
        if (!StringUtils.isEmpty(brandId)){
            criteria.andBrandIdEqualTo(brandId);
            criteria2.andBrandIdEqualTo(brandId);
        }
        if (!StringUtils.isEmpty(keyword)){
            criteria.andKeywordsLike("%"+keyword+"%");
            criteria2.andNameLike("%"+keyword+"%");
        }
        if (!StringUtils.isEmpty(isHot)){
            criteria.andIsHotEqualTo(isHot);
            criteria2.andIsHotEqualTo(isHot);
        }
        if (!StringUtils.isEmpty(isNew)){
            criteria.andIsNewEqualTo(isNew);
            criteria2.andIsNewEqualTo(isNew);
        }
        criteria.andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        criteria2.andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            example.setOrderByClause(sort+" "+order);
        }
        PageHelper.startPage(page,limit);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> queryByCatId(Integer catId, int offset, int limit, String sort, String order) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(catId).andDeletedEqualTo(false);
        example.setOrderByClause(sort+" "+order);
        PageHelper.startPage(offset,limit);

        return productMapper.selectByExample(example);
    }

    @Override
    public Product queryByProId(Integer id) {
        ProductExample example = new ProductExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        return productMapper.selectOneByExample(example);
    }

    @Override
    public List<Product> queryByCategory(List<Integer> categoryList, Integer offset, Integer limit, String sort, String order) {
        ProductExample example = new ProductExample();
        example.or().andCategoryIdIn(categoryList).andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        example.setOrderByClause(sort+" "+order);
        PageHelper.startPage(offset,limit);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> queryHotPro(Integer offset, Integer limit, String sort, String order) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andIsHotEqualTo(true).andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        example.setOrderByClause(sort+" "+order);
        PageHelper.startPage(offset,limit);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> queryNewPro(Integer offset, Integer limit,String sort,String order) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        criteria.andIsNewEqualTo(true).andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        example.setOrderByClause(sort+" "+order);
        PageHelper.startPage(offset,limit);
        return productMapper.selectByExample(example);
    }

    @Override
    public long countByExample(ProductExample example) {
        return 0;
    }

    @Override
    public int deleteByExample(ProductExample example) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
     return  productMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Object insert(Product record) {

        if (record.getName()==null || record.getName().trim().isEmpty()){
            return  ResponseUtil.badArgument();
        }
        String name = record.getName();
        if (checkExistByName(name)) {
            return ResponseUtil.fail(GOODS_NAME_EXIST, "商品名已经存在");
        }

        record.setGoodsSn(RandomUtils.createActive());
        record.setDeleted(Boolean.valueOf("0"));
        record.setAddTime(new Date());
        record.setUpdateTime(new Date());
        record.setDetail(record.getDetail());
        record.setBrief(record.getBrief());
        record.setGallery(record.getGallery());
        record.setIsHot(record.getIsHot());
        record.setKeywords(record.getKeywords());
        record.setIsOnSale(record.getIsOnSale());
        record.setIsNew(record.getIsNew());
        record.setUnit(record.getUnit());
        record.setCounterPrice(record.getCounterPrice());
        record.setRetailPrice(record.getRetailPrice());
        productMapper.insert(record);
        return ResponseUtil.ok();
    }



    @Override
    public int updateByPrimaryKey(Product record) {
        if (record.getName()==null || record.getName().trim().isEmpty()){
            return  ResponseUtil.errs();
        }
        record.setUpdateTime(new Date());
        return productMapper.updateByPrimaryKey(record);
    }

    @Override
    public Object list(Integer goodsId, String goodsSn, String name, Integer page, Integer limit, String sort, String order) {

        List<Product> productList = querySelective(goodsId, goodsSn, name, page, limit, sort, order);
        return ResponseUtil.okList(productList);
    }

    @Override
    public int count() {
        ProductExample example = new ProductExample();
        example.or().andDeletedEqualTo(false);
        return (int) productMapper.countByExample(example);
    }

    @Override
    public Object detail(Integer id) {
        Product product =  findById(id);
        Map<String, Object> data = new HashMap<>();
        data.put("product", product);
        return ResponseUtil.ok(data);
    }

    @Override
    public List<Product> queryByIds(Integer[] ids) {
        ProductExample example = new ProductExample();
        ArrayList<Product> productList = new ArrayList<>();
        for (Integer id : ids) {
            example.or().andIdEqualTo(id).andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
            Product product = productMapper.selectByPrimaryKey(id);
            productList.add(product);
        }
        return productList;
    }

    public List<Product> querySelective(Integer goodsId, String goodsSn, String name, Integer page, Integer limit, String sort, String order) {
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();

        if (goodsId != null) {
            criteria.andIdEqualTo(goodsId);
        }
        if (!StringUtils.isEmpty(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page,limit);
        return productMapper.selectByExampleWithBLOBs(example);

    }

    public boolean checkExistByName(String name) {
        ProductExample example = new ProductExample();
        example.or().andNameEqualTo(name).andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
        return productMapper.countByExample(example) != 0;
    }


    /**
     * 获取某个商品信息,包含完整信息
     *
     * @param id
     * @return
     */
    public Product findById(Integer id) {
        ProductExample example = new ProductExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
       return productMapper.selectOneByExample(example);

    }

}
